<?php
namespace app\common\model;
use think\Model;
use EasyWeChat\Factory;
use think\model\concern\SoftDelete;

class Wechat extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;

    protected function base($query)
    {

    }
    //公众号API
    public static function wechat()
    {
        $options = System::get_config_by_code('wechat');
        $log = [
            'level' => 'debug',
            'file' => \think\facade\Env::get('runtime_path').'wechat.log',
        ];
        /**
         * OAuth 配置
         *
         * scopes：公众平台（snsapi_userinfo / snsapi_base），开放平台：snsapi_login
         * callback：OAuth授权完成后的回调页地址
         */
        $oauth = [
            'scopes' => ['snsapi_userinfo'],
            'callback' => '/callback.html',
        ];
        $options['log'] = $log;
        $options['oauth'] = $oauth;
        return Factory::officialAccount($options);
    }
    //公众号微信支付
    public static function wxpay()
    {
        $options = System::get_config_by_code('wechat');
        $log = [
            'level' => 'debug',
            'file' => \think\facade\Env::get('runtime_path').'wechat.log',
        ];
        $options['log'] = $log;
        return Factory::payment($options);
    }
    //小程序API
    public static function wxapp()
    {
        $options = System::get_config_by_code('wxapp');
        $log = [
            'level' => 'debug',
            'file' => \think\facade\Env::get('runtime_path').'wechat.log',
        ];
        $options['log'] = $log;
        return Factory::miniProgram($options);
    }

    public static function get_access_token()
    {
        $accessToken = self::wechat()->access_token->getToken();
        return $accessToken;
    }

}